package com.proj.rest.resource;

import com.proj.beans.User;
import com.proj.core.ServiceFactory;
import com.proj.service.UserService;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public class UsersResource
{
    private UserService userService;

    public UsersResource()
    {
        System.out.println("UsersResource.UsersResource.constructor");
    }

    @GET
    public List<User> getAllUsers()
    {
        System.out.println("this.userService = " + this.userService);
        if (userService == null)
        {
            //todo: remove this. There is a bug with the userresource being created for each request.
            System.out.println("Getting service in static way");
            userService = (UserService) ServiceFactory.getServiceFactory().getBean("UserService");
            if (userService == null)
            {
                throw new RuntimeException("Unable to get userservice");
            }
        }
        return userService.getAllUsers();
    }

    public void setUserService(UserService userService)
    {
        System.out.println("UsersResource.setUserService=>" + userService);
        this.userService = userService;
        System.out.println("this.userService = " + this.userService);
    }
}
